<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col :xs="24" :sm="24" :md="8" :lg="6" :xl="5" style="margin-bottom: 10px">
        <el-card class="box-card">
          <div slot="header" class="clearfix">
            <span>个人信息</span>
          </div>
          <div>
            <div style="text-align: center">
              <el-upload
                :show-file-list="false"
                :on-success="oss.handleSuccess"
                :on-error="oss.handleError"
                :before-upload="oss.handleBeforeUpload"
                :http-request="oss.upload"
                action=""
                class="avatar-uploader"
              >
                <img :src="user.userIcon ? getPrefix() + user.userIcon : Avatar" title="点击上传头像" class="avatar">
              </el-upload>
            </div>
            <ul class="user-info">
              <li><div style="height: 100%"><svg-icon icon-class="login" /> 登录账号<div class="user-right">{{ user.userName }}</div></div></li>
              <li><svg-icon icon-class="phone" /> 手机号码 <div class="user-right">{{ user.userPhone }}</div></li>
              <li><svg-icon icon-class="email" /> 用户邮箱 <div class="user-right">{{ user.userEmail }}</div></li>
              <li>
                <svg-icon icon-class="anq" /> 安全设置
                <div class="user-right">
                  <a @click="$refs.pass.dialog = true">修改密码</a>
                </div>
              </li>
            </ul>
          </div>
        </el-card>
      </el-col>
      <el-col :xs="24" :sm="24" :md="16" :lg="18" :xl="19">
        <!--    用户资料    -->
        <el-card class="box-card">
          <el-tabs v-model="activeName" @tab-click="handleClick">
            <el-tab-pane label="用户资料" name="first">
              <el-form ref="form" :model="form" :rules="rules" style="margin-top: 10px;" size="small" label-width="65px">
                <el-form-item label="用户名" prop="nickName">
                  <el-input v-model="form.userName" style="width: 35%" />
                  <span style="color: #C0C0C0;margin-left: 10px;">用户名作为登陆使用</span>
                </el-form-item>
                <el-form-item label="手机号" prop="phone">
                  <el-input v-model="form.userPhone" style="width: 35%;" />
                  <span style="color: #C0C0C0;margin-left: 10px;">手机号码不能重复</span>
                </el-form-item>
                <el-form-item label="">
                  <el-button :loading="saveLoading" size="mini" type="primary" @click="doSubmit">保存配置</el-button>
                </el-form-item>
              </el-form>
            </el-tab-pane>
            <!--    操作日志    -->
            <el-tab-pane label="操作日志" name="second">
              <user-log ref="log" />
            </el-tab-pane>
          </el-tabs>
        </el-card>
      </el-col>
    </el-row>
    <update-pass ref="pass" />
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import updatePass from './module/updatePass'
import store from '@/store'
import { isvalidPhone } from '@/utils/validate'
import Avatar from '@/assets/images/avatar.png'
import UserLog from './module/log/index'
import OSS, { presenter } from '@/components/Oss/oss'
import { changeUserIcon, editUserInfo } from '@/api/system/user/user'
import { getPrefix } from '@/utils/upload-oss'

const defaultOss = OSS({ pathOssOnline: 'wdl/' })

export default {
  name: 'Center',
  components: { updatePass, UserLog },
  mixins: [presenter(defaultOss)],
  data() {
    // 自定义验证
    const validPhone = (rule, value, callback) => {
      if (!value) {
        callback(new Error('请输入电话号码'))
      } else if (!isvalidPhone(value)) {
        callback(new Error('请输入正确的11位手机号码'))
      } else {
        callback()
      }
    }
    return {
      Avatar: Avatar,
      activeName: 'first',
      saveLoading: false,
      form: {},
      rules: {
        userName: [
          { required: false, message: '请输入用户昵称', trigger: 'blur' },
          { min: 2, max: 20, message: '长度在 2 到 20 个字符', trigger: 'blur' }
        ],
        userPhone: [
          { required: false, trigger: 'blur', validator: validPhone }
        ]
      }
    }
  },
  computed: {
    ...mapGetters([
      'user'
    ])
  },
  created() {
    this.form = { userName: this.user.userName, userPhone: this.user.userPhone }
    store.dispatch('GetInfo').then(() => {})
  },
  methods: {
    [OSS.HOOK.uploadSuccess](data, optFile) {
      changeUserIcon(optFile.relativePath).then(() => {
        store.dispatch('GetInfo').then(() => {})
      })
    },
    [OSS.HOOK.uploadError](data, optFile) {},
    handleClick(tab, event) {
      if (tab.name === 'second') {
        this.$refs['log'].refresh()
      }
    },
    getPrefix,
    doSubmit() {
      if (this.$refs['form']) {
        this.$refs['form'].validate((valid) => {
          if (valid) {
            this.saveLoading = true
            editUserInfo(this.form).then(() => {
              this.$notify({ title: '修改成功', type: 'success', duration: 2500 })
              store.dispatch('GetInfo').then(() => {})
              this.saveLoading = false
            }).catch(() => {
              this.saveLoading = false
            })
          }
        })
      }
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss">
  .avatar-uploader-icon {
    font-size: 28px;
    width: 120px;
    height: 120px;
    line-height: 120px;
    text-align: center
  }

  .avatar {
    width: 120px;
    height: 120px;
    display: block;
    border-radius: 50%
  }
  .user-info {
    padding-left: 0;
    list-style: none;
    li{
      border-bottom: 1px solid #F0F3F4;
      padding: 11px 0;
      font-size: 13px;
    }
    .user-right {
      float: right;

      a{
        color: #317EF3;
      }
    }
  }
</style>
